<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title></title>
<jsp:include page="/WEB-INF/views/include/newhead.jsp" />
<link href="${ctxStatic}/common/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
	<div style="padding-right: 20px; padding-left: 20px; padding-top: 5px;padding-bottom: 5px;">
	    <c:if test="${empty params}">
		<jsp:include page="${includeUrl}" >
		<jsp:param value="${singleSelect}" name="singleSelect"/>
		</jsp:include>
		</c:if>
		<c:if test="${not empty params}">
		<jsp:include page="${includeUrl}" >
		    <jsp:param value="${params}" name="params"/>
		    <jsp:param value="${singleSelect}" name="singleSelect"/>
		</jsp:include>
		</c:if>
	</div>
	<div class="clear"></div>

	<div class="box-btn" style="padding-right: 20px; padding-left: 20px;">
		<div class="hasselect selectlabel" style="width: 100%">
		</div>
	</div>
	
	<input id="selectIds" type="hidden"  value="${selectIds}" />
	<input id="selectNames" type="hidden"  value="${selectNames}" />
	
</body>


<script type="text/javascript">
	var $table = $('#table');

	//点击触发事件
	$(function() {
		
		 initSelectValue();
		
		//选中一个
		$table.on('check.bs.table', function(e, row, element) {
			
			//alert('getSelections: '
			//		+ JSON.stringify(row));
			//alert('getSelections: '
			//		+ JSON.stringify($table.bootstrapTable('getSelections'))); 
			var singleSelect = '${singleSelect}';
			var row_data;
			
			//单选
			if(singleSelect == 'true'){
				$(".hasselect").empty();
				var row_date_arr = $table.bootstrapTable('getSelections');
				$.each(row_date_arr,function (index,row){
					row_data = JSON.stringify(row);
				})
				
			}
			
			var select_id = '#select-' + row.id;
			var select_id_value = $(select_id).val();
			
			   if(select_id_value =='undefined' || select_id_value =='' || select_id_value == null){
				   
				   $(".hasselect").append("<span>"+ row['${tableSelectName}']+
							"<i>×</i>"+
							"<input id='select-"+row['${tableSelectId}'] +"' class='select-id' type='hidden' value='" + row['${tableSelectId}'] +"'/>"+
							"<input type='hidden' class='select-name' value='"+ row['${tableSelectName}'] + "'/>"+
							"<input type='hidden' class='select-row-data' value='"+ row_data + "'/>" +
							"</span>"
			    );
				   
			  }
			
			removeselect();
		});
		
		//选择多个
		$table.on('check-all.bs.table', function(e, rows, element) {
			
			$.each(rows,function (index,row){
				var select_id = '#select-' + row.id;
				var select_id_value = $(select_id).val();
			     //没有数据，则往里面插入数据
				if(select_id_value =='undefined' || select_id_value =='' || select_id_value == null){
					   
					   $(".hasselect").append("<span>"+ row['${tableSelectName}']+
								"<i>×</i>"+
								"<input id='select-"+row['${tableSelectId}'] +"' class='select-id' type='hidden' value='" + row['${tableSelectId}'] +"'/>"+
								"<input type='hidden' class='select-name' value='"+ row['${tableSelectName}'] + "'/>"+
								"<input type='hidden' class='select-row-data' value=''/>" +
								"</span>"
				    );
					    
			    }
			})
			
			removeselect();
		});
		
		//去除一个
		$table.on('uncheck.bs.table', function(e, row, element) {
			
			//console.log(row.id);
			var rowId = row.id
			$("#select-"+rowId).parent().remove();
			
			removeselect();
			 
		});
		
		//去除多个
		$table.on('uncheck-all.bs.table', function(e, rows, element) {
			
			$.each(rows,function (index,row){
				
				var rowId = row.id
				$("#select-"+rowId).parent().remove();
			})
			
			removeselect();
		});
		
		
		//获取被选中的id
	    function getIdSelections() { 
	    	        return $.map($table.bootstrapTable('getSelections'), function (row) { 
	    	             return row.id 
	    	         }); 
	   }
		
	  removeselect();
	  function removeselect(){
			  $(".selectlabel i").click(function(){
				   $(this).parent().remove();
			   });
		  }
	  
	});
	
	
	  //显示默认选择值
	  function initSelectValue(){
		 
		 var selectIds = $("#selectIds").val();
		 var selectNames = $("#selectNames").val();
		 
		 var selectArr = new Array(); //定义一数组 
		 selectArr= selectIds.split(","); //字符分割 
		 
		 var selectNameArr = new Array(); //定义一数组 
		 selectNameArr= selectNames.split(","); //字符分割 
		
		
		 for (i=0;i<selectArr.length ;i++ ){ 
			   if(selectArr[i] !=''){
				   $(".hasselect").append("<span>"+ selectNameArr[i]+
							"<i>×</i>"+
							"<input id='select-"+ selectArr[i] +"' class='select-id' type='hidden' value='" + selectArr[i] +"'/>"+
							"<input type='hidden' class='select-name' value='"+ selectNameArr[i] + "'/>"+
							"<input type='hidden' class='select-row-data' value=''/>" +
							"</span>");   
			   }

		 }
		
	  }
</script>

</html>